<template>
  <div class="page-container">
    <van-nav-bar  class="nar-bar" >
      <div class="nav-search-bar" slot="title" @click="$router.push('/search')">
      <i slot="icon" class="iconfont icon-sousuo"></i> <span>搜索</span>
      </div>
    </van-nav-bar>
    <van-tabs v-model="activeTab"  class="home-tab">
       <van-tab :title="channels.name" v-for="(channels, index) in channels" :key="index" class="home-tab-item">
         <article-list :channelId="channels.id" ></article-list>
           </van-tab>
            <i slot="nav-right" class="iconfont icon-gengduo" @click="setChannelsPopup = true"></i>
  </van-tabs>
    <van-popup v-model="setChannelsPopup"
    position="bottom"
    closeable
    close-icon-position="top-left"
    style="height: 100%"
    >
      <channel-edit :mychannels="channels" :activeTab="activeTab" @switchTab="switchTab"></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/channels'
import articleList from './comments/article-list.vue'
import ChannelEdit from './comments/channel-edit.vue'
import { mapGetters } from 'vuex'
import { getItem } from '@/utils/setToken'
export default {
  components: { articleList, ChannelEdit },
  name: 'home',
  data () {
    return {
      activeTab: 0,
      channels: [],
      setChannelsPopup: false
    }
  },
  methods: {
    async getUserChannels () {
      let channels
      if (this.isLogin) {
        const data = await getUserChannels()
        channels = data.channels
      } else {
        channels = getItem('TOUTIAO_CHANNELS')
        if (!channels) {
          const data = await getUserChannels()
          channels = data.channels
        }
      }
      this.channels = channels
    },
    switchTab ({ active, popup }) {
      this.activeTab = active
      this.setChannelsPopup = false
      this.setChannelsPopup = popup
    }
  },
  computed: {
    ...mapGetters(['isLogin'])
  },
  mounted () {
    this.getUserChannels()
  }
}
</script>

<style lang="scss" scoped>
  .nar-bar{
    background: #3296FA;
    height: 88px;
     ::v-deep .van-nav-bar__title {
    max-width: unset;
  }
    .nav-search-bar{
      width: 555px;
      height: 64px;
      background: rgba(255,255,255, .2);
      border-radius: 32px;
      line-height: 64px;
      font-size: 28px;
      color: #fff;
      .iconfont{
        font-size: 32px;
      }
    }
  }
  .home-tab{
    ::v-deep .van-tabs__wrap{
      border-bottom: 1px solid #EDEFF3;
      height: 80px;
       .van-tabs__nav{
         padding-bottom: 0;
       }
      .van-tab{
      border-right: 1px solid #EDEFF3;
      width: 200px;
    }
    .van-tabs__line{
      background: #3296FA;
      width: 31px;
      height: 6px;
      bottom: 8px;
    }
     .van-tab--active .van-tab__text {
      color: #333333;
      font-size: 28px;
      font-weight: bold;
    }
    .van-tab__text {
      color: #777777;
      font-size: 28px;
    }
     .icon-gengduo {
    position: fixed;
    right: 0;
    width: 66px;
    font-size: 40px;
    color: #333333;
    height: 82px;
    line-height: 82px;
    text-align: center;
    background: rgba(255,255,255, .9);
  }
  }
}
</style>
